<?php

class index extends bhCommon {

    public function __construct()
    {
        parent::__construct();
    }



    public function home()
    {
        $pattern = getConfig('pattern', 2);


        $images = array(
            'home_bg_image' => $this->images('home_bg_image', '3.png'),
            'login_image' => $this->images('login_image', 'login.png'),
            'resize' => $this->images('click_resize', 'resize.png'),
            'wave_bot' => $this->image_url . 'wave-bot.png',
            'wave_mid' => $this->image_url . 'wave-mid.png',
            'wave_top' => $this->image_url . 'wave-top.png',
            'more' => $this->image_url . 'more.png',
        );



        $roll_content = p_getall('roll');
        if ($roll_content) {
            foreach ($roll_content as & $i) {
                $i['image'] = getImage($i['image']);
            }
        }

        $captcha_url = $this->app->createUrl('distribute', array('contr' => 'clock', 'action' => 'captcha', 'token' => $this->get('token')));

        $config = array(
            'xcx_title' => getConfig('xcx_title'),
            'grid_ad' => getConfig('grid_ad'),
            'screen_ad' => getConfig('screen_ad'),
            'video_ad' => getConfig('video_ad'),
            'clock_num' => getConfig('clock_num', 20),
            'clock_interval' => getConfig('clock_interval', 20),
            'currency_name' => getConfig('currency_name', '健康币'),
            'clock_text' => getConfig('clock_text', '点击打卡'),
            'profit_subscribe_id' => getConfig('profit_subscribe_id'),
            'roll_content_bg' => getConfig('roll_content_bg'),
            'ad_type' => getConfig('ad_type', 1),
            'xcx_display_type' => getConfig('xcx_display_type', 1),
            'click_bg_color' => getConfig('click_bg_color', '#fffcf7'),
            'roll_content' => $roll_content,
            'isChason' => $this->rising->isChason(),
            'captcha_url' => $captcha_url,
            'is_captcha' => getConfig('is_captcha', 1),
            'single_ad' => getConfig('single_ad'),
			'home_group_id' => getConfig('home_group_id', '932136843'),
            'first_show_img' => getImage(getConfig('first_show_img')),
            'pattern' => $pattern
        );
        $config['ad_show_type'] = $this->rising->get_ad_type($config['ad_type']);


        $subsection = [];
        if ($pattern == 2) {
            $images['new_home_bg_image'] = $this->images('new_home_bg_image', 'new_home_bg.png');
            $images['new_home_image'] = $this->images('new_home_image', 'new_image.png');
            $images['currency_image'] = $this->images('currency_image', 'currency_imgage.png');
            $config['new_home_txt'] = getConfig('new_home_txt');
            $config['new_home_txt_color'] = getConfig('new_home_txt_color', '#F6F28B');

            $subsection = $this->rising->subsection($this->member['id']);
        }

        $rule = p_getall('rule', array('position' => 1), array(), '', 'sort desc');

        $program = p_getall('program', array('position' => 2), array(), '', 'sort desc');

        $program = array_map('ad', $program);
        $isQqhome = $this->rising->isQqhome();
        $goods = array();
        if ($isQqhome) {
            $goods = p_getall('goods', array('is_home' => 2), array(), '', 'sort desc');
            $goods = arrayImage($goods, 'cover_image');
        }

        $data = array(
            'images' => $images,
            'config' => $config,
            'rule' => $rule,
            'program' => $program,
            'member' => $this->member,
            'is_auth' => $this->is_auth,
            'time' => strtotime(date('Y-m-d')),
            'isDouble' => $this->rising->isOpenDouble(),
            'isPopularitye' => $this->rising->isPopularitye(),
            'is_time_slot' => $this->rising->is_time_slot(),
            'isQqhome' => $isQqhome,
            'goods' => $goods,
            'subsection' => $subsection,
            'loading' => true
        );

        if ($data['isPopularitye'] && $this->member['is_first'] == 1) {
            p_update('member', array('is_first' => 2), array('id' => $this->member['id']));
        }
        $this->json($data);
    }


    /**
     *
     */
    public function today()
    {
        $today = $this->rising->get_clock_today($this->member['id']);

        $t = strtotime(date('Y-m-d'));

        $sql = "SELECT COUNT(*) FROM " . prefix('day') . " WHERE same_day={$t} AND clock > 0" . get_uniacid();
        $all_num = p_fetchcolumn($sql) ? : 0;

        $clock_user = array();
        if ($all_num > 0) {
            $l = $all_num > 6 ? 6 : $all_num;
            $sql = "SELECT B.head FROM " . prefix('day') . " AS A INNER JOIN " . prefix('member') . " AS B ON A.member_id=B.id WHERE A.same_day={$t} AND A.clock > 0 AND A.uniacid=" . UNIACID . " AND B.head!='' ORDER BY RAND() LIMIT {$l}";
            $clock_user = p_fetchall($sql);
        }

        $sql = "SELECT * FROM " . prefix('day') . " WHERE same_day={$t} " .get_uniacid() . " ORDER BY id ASC";
        $earliest = p_fetch($sql);

        if ($earliest) {
            $earliest['created'] = date('H:i:s', $earliest['created']);
            $earliest['member'] = p_get('member', array('id' => $earliest['member_id']));
        }

        $sql = "SELECT * FROM " . prefix('member') . " WHERE uniacid=" . UNIACID . " ORDER BY currency DESC";
        $palm = p_fetch($sql);

        $sql = "SELECT * FROM " . prefix('member') . " WHERE uniacid=" . UNIACID . " ORDER BY click DESC";
        $will = p_fetch($sql);

        $star = array(
            'earliest' => $earliest,
            'palm' => $palm,
            'will' => $will
        );

        $data = array(
            'today' => $today,
            'all_num' => $all_num,
            'clock_user' => $clock_user,
            'star' => $star
        );
        $this->json($data);
    }



    public function exemption()
    {
        $rule = p_getall('rule', array('position' => 2), array(), '', 'sort desc');
        $data = array(
            'rule' => $rule,
            'grid_ad' => getConfig('grid_ad'),
            'xcx_title' => getConfig('xcx_title'),
            'ad_type' => getConfig('ad_type', 1),
        );
        $data['ad_show_type'] = $this->rising->get_ad_type($data['ad_type']);
        $this->json($data);
    }


    /**
     *
     */
    public function program()
    {
        $program = p_getall('program', array('position' => 1), array(), '', 'sort desc');
        $program = array_map('ad', $program);

        $data = array(
            'program' => $program
        );
        $this->json($data);
    }


}